<template>
  <div class="index-lists">
    <el-card class="!border-none mt-4" shadow="never">
      <div style="display: flex; justify-content: space-between">
        <div>
          <!-- <el-button type="primary">
              <template #icon>
                <icon name="el-icon-Plus" />
              </template>
              添加联系人
            </el-button> -->
          <el-button v-perms="['linkman:add']" type="primary" @click="handleAdd()">
            <template #icon>
              <icon name="el-icon-Plus" />
            </template>
            添加联系人
          </el-button>
          <!--                    <el-button type="primary" @click="handleMerge()">-->
          <!--                        <template #icon>-->
          <!--                            <icon name="local-icon-chuangyiwuliao" />-->
          <!--                        </template>-->
          <!--                        合并联系人-->
          <!--                    </el-button>-->
          <!--                    <el-button type="primary">-->
          <!--                        <template #icon>-->
          <!--                            <icon name="local-icon-qudao" />-->
          <!--                        </template>-->
          <!--                        批量替换邮箱后缀-->
          <!--                    </el-button>-->
        </div>
        <el-form ref="formRef" :model="queryParams" :inline="true">
          <el-form-item label="联系人名称" prop="name">
            <el-input v-model="queryParams.name" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="resetPage">查询</el-button>
            <el-button @click="resetParams">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
        <el-table-column type="selection" width="55" />
        <el-table-column label="ID" prop="id" min-width="50" />
        <el-table-column
          label="案件类型"
          prop="caseType"
          min-width="150"
          show-overflow-tooltip="true"
        />
        <el-table-column label="姓名" prop="name" min-width="100" show-overflow-tooltip="true" />
        <el-table-column
          label="联系人类别"
          prop="type"
          min-width="120"
          show-overflow-tooltip="true"
        />
        <el-table-column label="座机" prop="number" min-width="100" show-overflow-tooltip="true" />
        <el-table-column label="手机" prop="phone" min-width="100" show-overflow-tooltip="true" />
        <el-table-column
          label="是否在职"
          prop="isOnJob"
          min-width="100"
          show-overflow-tooltip="true"
        />
        <el-table-column
          label="工作邮箱"
          prop="workEmail"
          min-width="100"
          show-overflow-tooltip="true"
        />
        <el-table-column
          label="私人邮箱"
          prop="email"
          min-width="100"
          show-overflow-tooltip="true"
        />
        <el-table-column label="联系地址" min-width="100" show-overflow-tooltip="true" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button v-perms="['linkman:edit']" type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button v-perms="['linkman:del']" type="danger" link @click="handleDelete(row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    <merge-popup v-if="showMerge" ref="mergeRef" @success="getLists" @close="showMerge = false" />
  </div>
</template>
<script lang="ts" setup name="linkman">
import { linkmanDelete, linkmanLists } from '@/api/linkman'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import MergePopup from './merge.vue'
import { CUSTOMER_ID } from '@/enums/cacheEnums'
import cache from '@/utils/cache'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const mergeRef = shallowRef<InstanceType<typeof MergePopup>>()
const showEdit = ref(false)
const showMerge = ref(false)
const queryParams = reactive({
  name: '',
  customerMianId: ''
})

queryParams.customerMianId = cache.get(CUSTOMER_ID)

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: linkmanLists,
  params: queryParams
})

const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleMerge = async () => {
  showMerge.value = true
  await nextTick()
  mergeRef.value?.open()
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await linkmanDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>
